/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
// pegheap.hpp : Dynamic memory management routines.
//
// These routines are potentially used in two locations:
//
// 1) To manage "extra" video memory for run-time allocated
//    bitmaps which are placed in video memory for fast
//    drawing.
//
// 2) To replace the compiler run-time library heap management
//    functions which are not task-safe. We do this replacement
//    only if you are running with an RTOS and that RTOS does not
//    provide its own variable size memory management service.
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/

#ifndef _PEG_HEAP_MANAGER_
#define _PEG_HEAP_MANAGER_

#define PEG_HEAP_USED_MAGIC 0xdeadbeefL
#define PEG_HEAP_FREE_MAGIC 0x12345678L
#define PEG_HEAP_NEWBLOCK_LIMIT 256


/*--------------------------------------------------------------------------*/
// PEG_HEAP_DEBUG
// Turn this on to provide extra API functions to do heap-walk type checking
// at any point in the application code. Default is to turn this off.
/*--------------------------------------------------------------------------*/
#define PEG_HEAP_DEBUG

struct PEG_MEM_BLOCK {
    PEGULONG Magic;
    PEG_MEM_BLOCK *pNext;
    PEG_MEM_BLOCK *pPrev;
    PEG_MEM_BLOCK *pNextFree;
    PEGULONG Size;
};

typedef struct {
    PEGUBYTE *pStart;
    PEGULONG Size;
    PEG_MEM_BLOCK *pFreeList;
} PEG_HEAP_CONTROL;

void PegHeapInitialize(PEG_HEAP_CONTROL *pControl);

void PegByteAllocate(PEG_HEAP_CONTROL *pControl,
    void **pMem, PEGULONG Size);

void PegByteFree(PEG_HEAP_CONTROL *pControl, void *pMem);


#if defined(PEG_HEAP_DEBUG)

PEGINT GetHeapFreeBlocks(PEG_HEAP_CONTROL *pControl);
PEGINT GetHeapUsedBlocks(PEG_HEAP_CONTROL *pControl);
PEGULONG GetHeapFreeMem(PEG_HEAP_CONTROL *pControl);
PEGULONG GetHeapUsedMem(PEG_HEAP_CONTROL *pControl);
PEGBOOL CountTotalHeap(PEG_HEAP_CONTROL *pControl);
PEGBOOL WalkHeap(PEG_HEAP_CONTROL *pControl);

#endif      // PEG_HEAP_DEBUG functions provided?

#endif      // sentry

// end of file




